# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from toolFunctions import response, checkLogin, validator
from django.core.exceptions import ObjectDoesNotExist
from django.forms.models import model_to_dict
from .models import Comment, CommentLikeUer
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
import json


#   添加评论
@checkLogin.check_login
def comment_add(request):
    user_id = request.session.get('user_id')

    json_result = json.loads(request.body)
    parent_id = json_result['comment_id'] or ''
    article_id = json_result['article_id'] or ''
    content = json_result['content'] or ''

    if request.method != 'POST':
        return response.fail('ERROR_RES_METHOD')

    if response.request_is_empty(article_id):
        return response.fail(message='文章编号不能为空')

    if response.request_is_empty(content):
        return response.fail(message='评论内容不能为空')

    comment = Comment(user_id=user_id, parent_id=parent_id, article_id=article_id, content=content)
    comment.save()

    return response.success(comment.id)


#   评论的评论列表
def comment_list(request):
    pageNo = request.GET.get('pageNo', 1)
    pageSize = request.GET.get('pageSize', 15)
    comment_id = request.GET.get('comment_id', '')
    user_id = request.session.get('user_id', '')

    if validator.is_empty(comment_id):
        return response.fail(message='评论编号不能为空')

    res = {}
    comments = Comment.objects.filter(status=1, parent_id=comment_id).order_by("-create_time")
    paginator = Paginator(comments, pageSize)
    res['total'] = paginator.count

    try:
        list = paginator.page(pageNo)
    except PageNotAnInteger:
        list = paginator.page(1)
    except EmptyPage:
        list = []

    arr_list = []
    for item in list:
        dict_item = model_to_dict(item)
        dict_item['create_time'] = item.create_time.strftime("%Y-%m-%d %H:%M:%S")
        dict_item['update_time'] = item.update_time.strftime("%Y-%m-%d %H:%M:%S")
        #   获取点赞数量以及用户是否点赞
        comment_like_user = CommentLikeUer.objects.filter(comment_id=dict_item['id'])
        dict_item['like_count'] = comment_like_user.values().count()
        dict_item['is_like'] = 0
        if user_id:
            dict_item['is_like'] = 0 if (comment_like_user.filter(user_id=user_id).first() is None) else 1
        #   获取评论人信息
        item_user = item.user
        dict_item['user'] = {
            'username': item_user.username,
            'id': item_user.id,
            'head_img': item_user.head_img
        }

        arr_list.append(dict_item)

    res['items'] = arr_list

    return response.success(res)


#   评论删除
@checkLogin.check_login
def comment_delete(request):
    user_id = request.session.get('user_id')
    comment_id = request.GET.get('comment_id', '')

    if response.request_is_empty(comment_id):
        return response.fail(message='评论编号不能为空')

    comment = Comment.objects.filter(id=comment_id).first()
    if comment is None:
        return response.fail(message='评论不存在')

    if comment.user.id != user_id:
        return response.fail(message='没有权限删除')

    comment.status = 0
    comment.save()

    return response.success()


#   评论点赞
@checkLogin.check_login
def comment_like(request):
    user_id = request.session.get('user_id')
    comment_id = request.GET.get('comment_id', '')

    if response.request_is_empty(comment_id):
        return response.fail(message='评论编号不能为空')

    comment = Comment.objects.filter(id=comment_id).first()
    if (comment is None) or comment.status == 0:
        return response.fail(message='评论不存在')

    if comment.user.id == user_id:
        return response.fail(message='不能点赞自己的评论哦')

    comment_like_user = CommentLikeUer.objects.filter(comment_id=comment_id, user_id=user_id).first()
    if comment_like_user:
        comment_like_user.delete()
    else:
        comment_like_user = CommentLikeUer(comment_id=comment_id, user_id=user_id)
        comment_like_user.save()

    return response.success()
